<template>
	<view class="pageBox">
		<view class="myBox-topBox">
			<view class="navBox">
				<uni-nav-bar height="88rpx" fixed title="我的积分" left-icon="left" @clickLeft="$back()" color="#000"
					:border="false" statusBar>
				</uni-nav-bar>
			</view>
			<view class="contentBox">
				<view class="availableBox">
					<view class="left">
						<view class="title">可用积分</view>
						<view class="availableNum">{{userInfo.integral||0}}</view>
						<view class="tipText">积分有效期每年12.31日自动清零</view>
					</view>
					<image src="@/static/integral/bg.png" class="bg" mode=""></image>
					<view class="bottomBtnList" style="z-index: 1;">
						<view class="availableBtn" @click="goto('common/integral/integralRank/integralRank')">
							排行榜</view>

					</view>
				</view>
			</view>
		</view>
		<!-- 对于mescroll-body: 需设置:sticky="true", 此应避免在mescroll-body标签前面加其他非定位的元素, 否则下拉区域会被挤出, 无法会隐藏.-->
		<!-- 对于mescroll-uni: 则无需设置:sticky="true", 无其他限制和要求 -->
		<mescroll-body :sticky="true" ref="mescrollRef" :top="mescroll_top" @init="mescrollInit" @down="downCallback"
			@up="upCallback" :up="upOption">
			<view class="integralRecordContentBox">
				<view class="integralRecordContent">
					<view class="integralRecordContent-title">
						<view class="titleX"></view>
						<view class="title">积分明细</view>
					</view>
					<view class="integralRecordRow" v-for="(item,index) in dataList.rows" :key="index">
						<view class="left">
							<view class="top">
								<image src="@/static/activity/activityScore.png" class="icon"></image>
								<view class="integralTitle u-line-1">{{item.integralTitle}}</view>
							</view>
							<view class="time">{{item.createTime}}</view>
						</view>
						<view class="right">
							<view class="integralText">{{item.integral}}</view>
						</view>
					</view>
				</view>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	import {
		getCommodityHomeGroupByType
	} from "@/api/goods.js"
	import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js'
	import * as systemApi from "@/api/system.js"
	import * as integralLogApi from "@/api/integralLog.js"
	let integralRecord_Search = {
		endTime: '',
		search: '',
		startTime: '',
		status: -2,
	}
	export default {
		mixins: [MescrollMixin], // 注意此处还需使用MescrollMoreItemMixin (必须写在MescrollMixin后面)
		// components: {
		// 	searchRow
		// },
		data() {
			return {
				dataInfo: {
					integral: 0
				},
				tabsStyle: {
					height: '88rpx',
					boxSizing: 'border-box'
				},
				tabsAStyle: {
					color: '#0138D2'
				},
				tabs: [{
						name: '审核中',
						id: 0
					},
					{
						name: '已通过 ',
						id: 1
					},
					{
						name: '不通过',
						id: 2
					},
					{
						name: '售后记录',
						id: -1
					}
				],
				tabIndex: 0,
				tabIndexOld: 0,
				options: {},
				mescroll_top: '340rpx',
				downOption: {
					auto: true // 不自动加载 (mixin已处理第一个tab触发downCallback)
				},
				upOption: {
					auto: false, // 不自动加载
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 8 // 每页数据的数量
					},
					noMoreSize: 4, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					textNoMore: '--这里是底线--',
					empty: {
						tip: '暂无' // 提示
					}
				},
				dataList: {
					records: [],
					rows: [],
					pages: 0,
					size: 0,
					total: 0,
					current: 0
				}, //列表数据
				listObj: {
					integralType: 0,
				},
				canReset: false,
				dicts: {
					consumption_type: {}
				},
				dictList: {
					consumption_type: []
				},
				userInfo: {
					userName: '',
					userType: '',
					roles: [{
						roleName: '',
						roleId: '',
						roleKey: ''
					}],
					avatar: '',
					nickName: '',
					sex: 2
				},
			}
		},
		mounted() {
			const that = this
			// // #ifdef MP
			// uni.getSystemInfo({
			// 	success: function(res) {
			// 		that.mescroll_top = 88 + 88 + res.statusBarHeight * 2 + 'rpx'
			// 	}
			// })
			// // #endif
		},
		onLoad(options) {
			this.options = options
			this.getDictDataList('consumption_type')
		},
		mounted() {
			// this.setMescrollTop()
		},
		onShow() {
			// that.canReset && that.getData()
			// that.canReset = true
			this.userInfo = this.$store.getters.my_userInfo
			if (!this.userInfo) {
				this.$store.dispatch('userBaseInfo').then(res => {
					this.userInfo = res.user
				})
			}

		},
		methods: {
			setMescrollTop() {
				const query = uni.createSelectorQuery().in(this);
				this.$nextTick(() => {
					setTimeout(() => {
						query.select(".myBox-topBox")
							.boundingClientRect((data) => {
								this.mescroll_top = data.height * 2 + 'rpx'
								console.log(this.mescroll_top);
							})
							.exec();
					}, 300)
				})
			},
			getDictDataList(dictType) {
				return new Promise((resolve, reject) => {
					systemApi.dictDataList(dictType).then(({
						rows
					}) => {
						let objs = {}
						let keyValues = []
						rows.forEach((item) => {
							objs[item.dictValue] = item.dictLabel
							keyValues.push({
								label: item.dictLabel,
								text: item.dictLabel,
								value: item.dictValue
							})
						})
						this.dicts[dictType] = objs
						this.dictList[dictType] = keyValues
						resolve({
							objs,
							keyValues,
							rows
						})
					}).catch(reject)
				})
			},
			goto(url, data) {
				this.$navTo(`pages/${url}`, data)
			},
			//手动刷新
			getData() {
				let curMescroll = this.$refs.mescrollItem[0].mescroll
				curMescroll && curMescroll.resetUpScroll()
				curMescroll && curMescroll.scrollTo(0, 0)
			},
			tabsChange(e) {
				this.tabIndexOld = this.tabIndex
				this.tabIndex = e.index
				this.listObj.integralType = e.value
				uni.setStorageSync('integralRecord_Search', JSON.stringify({
					integralType: this.listObj.integralType,
				}))
				this.downCallback()
			},
			/*下拉刷新的回调 */
			downCallback(page) {
				// 这里加载你想下拉刷新的数据, 比如刷新轮播数据
				this.setMescrollTop();
				// 下拉刷新的回调,默认重置上拉加载列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
				this.mescroll.resetUpScroll()
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				//联网加载数据
				const that = this
				if (that.$store.getters.token) {
					this.getDataList({
						pageNum: page.num,
						pageSize: page.size,
						...that.listObj
					}).then(list => {
						const curPageLen = list.rows.length
						const totalSize = list.total
						that.mescroll.endBySize(curPageLen, totalSize)
					}).catch(err => {
						that.mescroll.endErr()
					})
				} else {
					that.mescroll.endBySize(0, 0)
					return
				}
			},
			/**
			 * @param {Object} page 页码
			 */
			getDataList(pageObj) {
				console.log(pageObj);
				const that = this
				return new Promise((resolve, reject) => {
					integralLogApi.integralLogList(pageObj).then((data) => {
						let dataList = JSON.parse(JSON.stringify(that.dataList))
						dataList.pageSize = pageObj.pageSize
						dataList.pageNum = pageObj.pageNum
						dataList.total = data.total
						dataList.pages = Math.ceil(data.total / dataList.pageSize)
						// 合并新数据
						const newList = data.rows
						// 如果是第一页需手动制空列表
						if (pageObj.pageNum == 1) dataList.rows = []
						// 合并新数据
						dataList.rows = dataList.rows.concat(newList)
						that.dataList = dataList
						resolve(that.dataList)
					}).catch(reject)
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import "@/pages/integral/integralScss.scss";



	.pageBox {
		background-color: #F4F7FF;

		.tabsBG {
			padding-left: 30rpx;
			background-color: #F4F7FF;
		}


		.myBox-topBox {
			position: fixed;
			top: 0;
			left: 0;
			// background: linear-gradient(90deg, #6FA9FF 100%, #6FA9FF 81%);
		}

		.contentBox {
			margin: 0;

			.availableBox {
				position: relative;
				display: flex;
				justify-content: space-between;
				margin-bottom: 0;
				border-radius: 24rpx 24rpx 0 0;
				background: linear-gradient(180deg, #008AFF 0%, #86C9FF 100%);
				border-radius: 16rpx 16rpx 16rpx 16rpx;

				.bg {
					width: 217rpx;
					height: 217rpx;
					position: absolute;
					right: 0;
					top: 0;
					z-index: 0;
				}
			}
		}

		.integralRecordContentBox {
			padding: 30rpx;

			.integralRecordContent {
				background: #FFFFFF;
				border-radius: 24rpx;
				padding: 30rpx;

				.integralRecordContent-title {
					display: flex;
					align-items: center;
					margin-bottom: 30rpx;

					.titleX {
						width: 6rpx;
						height: 32rpx;
						background: #6FA9FF;
						border-radius: 0rpx 0rpx 0rpx 0rpx;
						margin-right: 16rpx;
					}

					.title {
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 28rpx;
						color: #000000;
					}
				}
			}

			.integralTitle {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 28rpx;
				color: #000000;
			}
		}

		.integralRecordRow {
			// height: 364rpx;
			background: #FFFFFF;
			border-radius: 16rpx;
			margin-bottom: 30rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.left {
				.top {
					display: flex;
					align-items: center;
					margin-bottom: 14rpx;

					.icon {
						width: 40rpx;
						height: 40rpx;
						margin-right: 16rpx;
						flex-shrink: 0;
					}
				}

				.time {
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #979797;
					padding-left: 56rpx;
				}
			}



			.integralText {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 32rpx;
				color: #FF0000;
			}



			.row {
				display: flex;
				justify-content: space-between;
				align-items: flex-start;
			}

			.orderStatus {
				margin-left: 20rpx;
				flex-shrink: 0;
				font-size: 28rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
			}
		}
	}
</style>